﻿using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for PublishToFileSystemWindow.xaml
    /// </summary>
    public partial class PublishToFileSystemWindow : Window
    {
        public PublishToFileSystemWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(_ctlPublishDir.Text))
            {
                if (!Directory.Exists(_ctlPublishDir.Text))
                {
                    CustomMessageBox.ShowWarning("所选目录不存在");
                    return;
                }
                PublishToFileSystemThread.Start(Designtime.Metadata.LocalStorage.GetSolutionDir(UIContext.Current.LocalWorkSpace.LocalPath, UIContext.Current.CurrentSolution.ID), UIContext.Current.CurrentSolution, _ctlPublishDir.Text, Completed);
                ProgressbarWindow.Instance.ShowProgress(true);
            }
            else
            {
                CustomMessageBox.ShowWarning("请选择发布目录");
            }
        }

        public void Completed(bool result)
        {
            ProgressbarWindow.Instance.Complete();
            Dispatcher.Invoke(() =>
            {
                if (result)
                {
                    CustomMessageBox.ShowInfo("发布成功");
                    this.DialogResult = true;
                }
                else
                {
                    CustomMessageBox.ShowError("发布失败");
                }
            });
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void selectDirButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folderBD = new System.Windows.Forms.FolderBrowserDialog();
            if (folderBD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (folderBD.SelectedPath != string.Empty)
                {
                    _ctlPublishDir.Text = string.Format("{0}\\", folderBD.SelectedPath);
                }
            }
        }
    }
}
